package com.ht.modules.system.online.codegenerate.database;

import com.ht.modules.system.online.codegenerate.a.ConfigVo;
import com.ht.modules.system.online.codegenerate.generate.pojo.ColumnVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.sql.*;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;

@Component
@Slf4j
public class DbReadTableUtil {
    private static Connection b;
    private static Statement c;
    private final ConfigVo configVo = new ConfigVo();
    public List<String> a() throws SQLException {
        return readAllTableNames();
    }

    public List<String> readAllTableNames() throws SQLException {
        String var1 = null;
        List<String> var2 = new ArrayList<>(0);

        try {
            Class.forName(configVo.diverName);
            b = DriverManager.getConnection(configVo.url, configVo.username, configVo.password);
            c = b.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
            String var3 = b.getCatalog();
            log.info(" connect databaseName : " + var3);
            if (com.ht.modules.system.online.codegenerate.database.a.a(configVo.url)) {
                var1 = MessageFormat.format("select distinct table_name from information_schema.columns where table_schema = {0}", com.ht.modules.system.online.codegenerate.generate.util.f.c(var3));
            }
            if (com.ht.modules.system.online.codegenerate.database.a.d(configVo.url)) {
                if (!configVo.schemaName.contains(",")) {
                    var1 = MessageFormat.format("select tablename from pg_tables where schemaname in( {0} )", com.ht.modules.system.online.codegenerate.generate.util.f.c(configVo.schemaName));
                } else {
                    StringBuilder var4 = new StringBuilder();
                    String[] var5 = configVo.schemaName.split(",");
                    for (String var9 : var5) {
                        var4.append(com.ht.modules.system.online.codegenerate.generate.util.f.c(var9)).append(",");
                    }

                    var1 = MessageFormat.format("select tablename from pg_tables where schemaname in( {0} )", var4.substring(0, var4.toString().length() - 1));
                }
            }
            ResultSet var0 = c.executeQuery(var1);
            while (var0.next()) {
                String var20 = var0.getString(1);
                var2.add(var20);
            }
        } catch (Exception var18) {
            var18.printStackTrace();
        } finally {
            try {
                if (c != null) {
                    c.close();
                    c = null;
                    System.gc();
                }

                if (b != null) {
                    b.close();
                    b = null;
                    System.gc();
                }
            } catch (SQLException var17) {
                throw var17;
            }

        }

        return var2;
    }

    public List<ColumnVo> a(String var0) throws Exception {
        String var2 = null;
        ArrayList var3 = new ArrayList();

        try {
            Class.forName(configVo.diverName);
            b = DriverManager.getConnection(configVo.url, configVo.username, configVo.password);
            c = b.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
            String var4 = b.getCatalog();
            log.info(" connect databaseName : " + var4);
            if (com.ht.modules.system.online.codegenerate.database.a.a(configVo.url)) {
                var2 = MessageFormat.format("select column_name,data_type,column_comment,numeric_precision,numeric_scale,character_maximum_length,is_nullable nullable from information_schema.columns where table_name = {0} and table_schema = {1} order by ORDINAL_POSITION", com.ht.modules.system.online.codegenerate.generate.util.f.c(var0), com.ht.modules.system.online.codegenerate.generate.util.f.c(var4));
            }

            if (com.ht.modules.system.online.codegenerate.database.a.b(configVo.url)) {
                var2 = MessageFormat.format(" select colstable.column_name column_name, colstable.data_type data_type, commentstable.comments column_comment, colstable.Data_Precision column_precision, colstable.Data_Scale column_scale,colstable.Char_Length,colstable.nullable from user_tab_cols colstable  inner join user_col_comments commentstable  on colstable.column_name = commentstable.column_name  where colstable.table_name = commentstable.table_name  and colstable.table_name = {0}", com.ht.modules.system.online.codegenerate.generate.util.f.c(var0.toUpperCase()));
            }

            if (com.ht.modules.system.online.codegenerate.database.a.d(configVo.url)) {
                var2 = MessageFormat.format("select icm.column_name as field,icm.udt_name as type,fieldtxt.descript as comment, icm.numeric_precision_radix as column_precision ,icm.numeric_scale as column_scale ,icm.character_maximum_length as Char_Length,icm.is_nullable as attnotnull  from information_schema.columns icm, (SELECT A.attnum,( SELECT description FROM pg_catalog.pg_description WHERE objoid = A.attrelid AND objsubid = A.attnum ) AS descript,A.attname \tFROM pg_catalog.pg_attribute A WHERE A.attrelid = ( SELECT oid FROM pg_class WHERE relname = {0} ) AND A.attnum > 0 AND NOT A.attisdropped  ORDER BY\tA.attnum ) fieldtxt where icm.table_name={1} and fieldtxt.attname = icm.column_name", com.ht.modules.system.online.codegenerate.generate.util.f.c(var0), com.ht.modules.system.online.codegenerate.generate.util.f.c(var0));
            }

            if (com.ht.modules.system.online.codegenerate.database.a.c(configVo.url)) {
                var2 = MessageFormat.format("select distinct cast(configVo.name as varchar(50)) column_name,  cast(b.name as varchar(50)) data_type,  cast(e.value as NVARCHAR(200)) comment,  cast(ColumnProperty(configVo.object_id,configVo.Name,'''Precision''') as int) num_precision,  cast(ColumnProperty(configVo.object_id,configVo.Name,'''Scale''') as int) num_scale,  configVo.max_length,  (case when configVo.is_nullable=1 then '''y''' else '''n''' end) nullable,column_id   from sys.columns ConfigVo left join sys.types b on configVo.user_type_id=b.user_type_id left join (select top 1 * from sys.objects where type = '''U''' and name ={0}  order by name) c on configVo.object_id=c.object_id left join sys.extended_properties e on e.major_id=c.object_id and e.minor_id=configVo.column_id and e.class=1 where c.name={0} order by configVo.column_id", com.ht.modules.system.online.codegenerate.generate.util.f.c(var0));
            }

            ResultSet var1 = c.executeQuery(var2);
            var1.last();
            int var5 = var1.getRow();
            if (var5 <= 0) {
                throw new Exception("该表不存在或者表中没有字段");
            }

            ColumnVo var7 = new ColumnVo();
            if (configVo.dbFiledConvert) {
                var7.setFieldName(com.ht.modules.system.online.codegenerate.generate.util.f.d(var1.getString(1).toLowerCase()));
            } else {
                var7.setFieldName(var1.getString(1).toLowerCase());
            }

            var7.setFieldDbName(var1.getString(1).toUpperCase());
            var7.setFieldType(com.ht.modules.system.online.codegenerate.generate.util.f.d(var1.getString(2).toLowerCase()));
            var7.setFieldDbType(com.ht.modules.system.online.codegenerate.generate.util.f.d(var1.getString(2).toLowerCase()));
            var7.setPrecision(var1.getString(4));
            var7.setScale(var1.getString(5));
            var7.setCharmaxLength(var1.getString(6));
            var7.setNullable(com.ht.modules.system.online.codegenerate.generate.util.f.a(var1.getString(7)));
            com.ht.modules.system.online.codegenerate.generate.util.f.a(var7);
            var7.setFiledComment(StringUtils.isBlank(var1.getString(3)) ? var7.getFieldName() : var1.getString(3));
            String[] var8 = new String[0];
            if (configVo.pageFilterFields != null) {
                var8 = configVo.pageFilterFields.toLowerCase().split(",");
            }

            if (!configVo.primaryKeyField.equals(var7.getFieldName()) && !com.ht.modules.system.online.codegenerate.database.util.a.a(var7.getFieldDbName().toLowerCase(), var8)) {
                var3.add(var7);
            }

            while (var1.previous()) {
                ColumnVo var9 = new ColumnVo();
                if (configVo.dbFiledConvert) {
                    var9.setFieldName(com.ht.modules.system.online.codegenerate.generate.util.f.d(var1.getString(1).toLowerCase()));
                } else {
                    var9.setFieldName(var1.getString(1).toLowerCase());
                }

                var9.setFieldDbName(var1.getString(1).toUpperCase());
                if (!configVo.primaryKeyField.equals(var9.getFieldName()) && !com.ht.modules.system.online.codegenerate.database.util.a.a(var9.getFieldDbName().toLowerCase(), var8)) {
                    var9.setFieldType(com.ht.modules.system.online.codegenerate.generate.util.f.d(var1.getString(2).toLowerCase()));
                    var9.setFieldDbType(com.ht.modules.system.online.codegenerate.generate.util.f.d(var1.getString(2).toLowerCase()));
                    var9.setPrecision(var1.getString(4));
                    var9.setScale(var1.getString(5));
                    var9.setCharmaxLength(var1.getString(6));
                    var9.setNullable(com.ht.modules.system.online.codegenerate.generate.util.f.a(var1.getString(7)));
                    com.ht.modules.system.online.codegenerate.generate.util.f.a(var9);
                    var9.setFiledComment(StringUtils.isBlank(var1.getString(3)) ? var9.getFieldName() : var1.getString(3));
                    var3.add(var9);
                }
            }
        } catch (ClassNotFoundException var18) {
            throw var18;
        } catch (SQLException var19) {
            throw var19;
        } finally {
            try {
                if (c != null) {
                    c.close();
                    c = null;
                    System.gc();
                }

                if (b != null) {
                    b.close();
                    b = null;
                    System.gc();
                }
            } catch (SQLException var17) {
                throw var17;
            }

        }

        List<ColumnVo> var21 = new ArrayList<>();

        for (int var22 = var3.size() - 1; var22 >= 0; --var22) {
            ColumnVo var6 = (ColumnVo) var3.get(var22);
            var21.add(var6);
        }

        return var21;
    }

    public List<ColumnVo> b(String tableName) throws Exception {
        return readOriginalTableColumn(tableName);
    }

    public List<ColumnVo> readOriginalTableColumn(String tableName) throws Exception {
        String var2 = null;
        ArrayList var3 = new ArrayList();

        try {
            Class.forName(configVo.diverName);
            b = DriverManager.getConnection(configVo.url, configVo.username, configVo.password);
            c = b.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
            String var4 = b.getCatalog();
            log.info(" connect databaseName : " + var4);
            if (com.ht.modules.system.online.codegenerate.database.a.a(configVo.url)) {
                var2 = MessageFormat.format("select column_name,data_type,column_comment,numeric_precision,numeric_scale,character_maximum_length,is_nullable nullable from information_schema.columns where table_name = {0} and table_schema = {1} order by ORDINAL_POSITION", com.ht.modules.system.online.codegenerate.generate.util.f.c(tableName), com.ht.modules.system.online.codegenerate.generate.util.f.c(var4));
            }

            if (com.ht.modules.system.online.codegenerate.database.a.b(configVo.url)) {
                var2 = MessageFormat.format(" select colstable.column_name column_name, colstable.data_type data_type, commentstable.comments column_comment, colstable.Data_Precision column_precision, colstable.Data_Scale column_scale,colstable.Char_Length,colstable.nullable from user_tab_cols colstable  inner join user_col_comments commentstable  on colstable.column_name = commentstable.column_name  where colstable.table_name = commentstable.table_name  and colstable.table_name = {0}", com.ht.modules.system.online.codegenerate.generate.util.f.c(tableName.toUpperCase()));
            }

            if (com.ht.modules.system.online.codegenerate.database.a.d(configVo.url)) {
                var2 = MessageFormat.format("select icm.column_name as field,icm.udt_name as type,fieldtxt.descript as comment, icm.numeric_precision_radix as column_precision ,icm.numeric_scale as column_scale ,icm.character_maximum_length as Char_Length,icm.is_nullable as attnotnull  from information_schema.columns icm, (SELECT A.attnum,( SELECT description FROM pg_catalog.pg_description WHERE objoid = A.attrelid AND objsubid = A.attnum ) AS descript,A.attname \tFROM pg_catalog.pg_attribute A WHERE A.attrelid = ( SELECT oid FROM pg_class WHERE relname = {0} ) AND A.attnum > 0 AND NOT A.attisdropped  ORDER BY\tA.attnum ) fieldtxt where icm.table_name={1} and fieldtxt.attname = icm.column_name", com.ht.modules.system.online.codegenerate.generate.util.f.c(tableName), com.ht.modules.system.online.codegenerate.generate.util.f.c(tableName));
            }

            if (com.ht.modules.system.online.codegenerate.database.a.c(configVo.url)) {
                var2 = MessageFormat.format("select distinct cast(configVo.name as varchar(50)) column_name,  cast(b.name as varchar(50)) data_type,  cast(e.value as NVARCHAR(200)) comment,  cast(ColumnProperty(configVo.object_id,configVo.Name,'''Precision''') as int) num_precision,  cast(ColumnProperty(configVo.object_id,configVo.Name,'''Scale''') as int) num_scale,  configVo.max_length,  (case when configVo.is_nullable=1 then '''y''' else '''n''' end) nullable,column_id   from sys.columns ConfigVo left join sys.types b on configVo.user_type_id=b.user_type_id left join (select top 1 * from sys.objects where type = '''U''' and name ={0}  order by name) c on configVo.object_id=c.object_id left join sys.extended_properties e on e.major_id=c.object_id and e.minor_id=configVo.column_id and e.class=1 where c.name={0} order by configVo.column_id", com.ht.modules.system.online.codegenerate.generate.util.f.c(tableName));
            }

            ResultSet var20 = c.executeQuery(var2);
            var20.last();
            int var5 = var20.getRow();
            if (var5 <= 0) {
                throw new Exception("该表不存在或者表中没有字段");
            }

            ColumnVo var7 = new ColumnVo();
            if (configVo.dbFiledConvert) {
                var7.setFieldName(com.ht.modules.system.online.codegenerate.generate.util.f.d(var20.getString(1).toLowerCase()));
            } else {
                var7.setFieldName(var20.getString(1).toLowerCase());
            }

            var7.setFieldDbName(var20.getString(1).toUpperCase());
            var7.setPrecision(com.ht.modules.system.online.codegenerate.generate.util.f.b(var20.getString(4)));
            var7.setScale(com.ht.modules.system.online.codegenerate.generate.util.f.b(var20.getString(5)));
            var7.setCharmaxLength(com.ht.modules.system.online.codegenerate.generate.util.f.b(var20.getString(6)));
            var7.setNullable(com.ht.modules.system.online.codegenerate.generate.util.f.a(var20.getString(7)));
            var7.setFieldType(com.ht.modules.system.online.codegenerate.generate.util.f.a(var20.getString(2).toLowerCase(), var7.getPrecision(), var7.getScale()));
            var7.setFieldDbType(com.ht.modules.system.online.codegenerate.generate.util.f.d(var20.getString(2).toLowerCase()));
            com.ht.modules.system.online.codegenerate.generate.util.f.a(var7);
            var7.setFiledComment(StringUtils.isBlank(var20.getString(3)) ? var7.getFieldName() : var20.getString(3));
            var3.add(var7);

            while (var20.previous()) {
                ColumnVo var8 = new ColumnVo();
                if (configVo.dbFiledConvert) {
                    var8.setFieldName(com.ht.modules.system.online.codegenerate.generate.util.f.d(var20.getString(1).toLowerCase()));
                } else {
                    var8.setFieldName(var20.getString(1).toLowerCase());
                }

                var8.setFieldDbName(var20.getString(1).toUpperCase());
                var8.setPrecision(com.ht.modules.system.online.codegenerate.generate.util.f.b(var20.getString(4)));
                var8.setScale(com.ht.modules.system.online.codegenerate.generate.util.f.b(var20.getString(5)));
                var8.setCharmaxLength(com.ht.modules.system.online.codegenerate.generate.util.f.b(var20.getString(6)));
                var8.setNullable(com.ht.modules.system.online.codegenerate.generate.util.f.a(var20.getString(7)));
                var8.setFieldType(com.ht.modules.system.online.codegenerate.generate.util.f.a(var20.getString(2).toLowerCase(), var8.getPrecision(), var8.getScale()));
                var8.setFieldDbType(com.ht.modules.system.online.codegenerate.generate.util.f.d(var20.getString(2).toLowerCase()));
                com.ht.modules.system.online.codegenerate.generate.util.f.a(var8);
                var8.setFiledComment(StringUtils.isBlank(var20.getString(3)) ? var8.getFieldName() : var20.getString(3));
                var3.add(var8);
            }
        } catch (ClassNotFoundException var17) {
            throw var17;
        } catch (SQLException var18) {
            throw var18;
        } finally {
            try {
                if (c != null) {
                    c.close();
                    c = null;
                    System.gc();
                }

                if (b != null) {
                    b.close();
                    b = null;
                    System.gc();
                }
            } catch (SQLException var16) {
                throw var16;
            }

        }

        ArrayList var21 = new ArrayList();

        for (int var22 = var3.size() - 1; var22 >= 0; --var22) {
            ColumnVo var6 = (ColumnVo) var3.get(var22);
            var21.add(var6);
        }

        return var21;
    }

    public boolean c(String var0) {
        String var2 = null;
        try {
            Class.forName(configVo.diverName);
            b = DriverManager.getConnection(configVo.url, configVo.username, configVo.password);
            c = b.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
            String var3 = b.getCatalog();
            log.info(" connect databaseName : " + var3);
            if (com.ht.modules.system.online.codegenerate.database.a.a(configVo.url)) {
                var2 = "select column_name,data_type,column_comment,0,0 from information_schema.columns where table_name = '" + var0 + "' and table_schema = '" + var3 + "'";
            }

            if (com.ht.modules.system.online.codegenerate.database.a.b(configVo.url)) {
                var2 = "select colstable.column_name column_name, colstable.data_type data_type, commentstable.comments column_comment from user_tab_cols colstable  inner join user_col_comments commentstable  on colstable.column_name = commentstable.column_name  where colstable.table_name = commentstable.table_name  and colstable.table_name = '" + var0.toUpperCase() + "'";
            }

            if (com.ht.modules.system.online.codegenerate.database.a.d(configVo.url)) {
                var2 = MessageFormat.format("select icm.column_name as field,icm.udt_name as type,fieldtxt.descript as comment, icm.numeric_precision_radix as column_precision ,icm.numeric_scale as column_scale ,icm.character_maximum_length as Char_Length,icm.is_nullable as attnotnull  from information_schema.columns icm, (SELECT A.attnum,( SELECT description FROM pg_catalog.pg_description WHERE objoid = A.attrelid AND objsubid = A.attnum ) AS descript,A.attname \tFROM pg_catalog.pg_attribute A WHERE A.attrelid = ( SELECT oid FROM pg_class WHERE relname = {0} ) AND A.attnum > 0 AND NOT A.attisdropped  ORDER BY\tA.attnum ) fieldtxt where icm.table_name={1} and fieldtxt.attname = icm.column_name", com.ht.modules.system.online.codegenerate.generate.util.f.c(var0), com.ht.modules.system.online.codegenerate.generate.util.f.c(var0));
            }

            if (com.ht.modules.system.online.codegenerate.database.a.c(configVo.url)) {
                var2 = MessageFormat.format("select distinct cast(configVo.name as varchar(50)) column_name,  cast(b.name as varchar(50)) data_type,  cast(e.value as NVARCHAR(200)) comment,  cast(ColumnProperty(configVo.object_id,configVo.Name,'''Precision''') as int) num_precision,  cast(ColumnProperty(configVo.object_id,configVo.Name,'''Scale''') as int) num_scale,  configVo.max_length,  (case when configVo.is_nullable=1 then '''y''' else '''n''' end) nullable,column_id   from sys.columns ConfigVo left join sys.types b on configVo.user_type_id=b.user_type_id left join (select top 1 * from sys.objects where type = '''U''' and name ={0}  order by name) c on configVo.object_id=c.object_id left join sys.extended_properties e on e.major_id=c.object_id and e.minor_id=configVo.column_id and e.class=1 where c.name={0} order by configVo.column_id", com.ht.modules.system.online.codegenerate.generate.util.f.c(var0));
            }

            ResultSet var1 = c.executeQuery(var2);
            var1.last();
            int var4 = var1.getRow();
            return var4 > 0;
        } catch (Exception var5) {
            var5.printStackTrace();
            return false;
        }
    }

    public String d(String var0) {
        String[] var1 = var0.split("_");
        var0 = "";
        int var2 = 0;

        for (int var3 = var1.length; var2 < var3; ++var2) {
            if (var2 > 0) {
                String var4 = var1[var2].toLowerCase();
                var4 = var4.substring(0, 1).toUpperCase() + var4.substring(1, var4.length());
                var0 = var0 + var4;
            } else {
                var0 = var0 + var1[var2].toLowerCase();
            }
        }

        var0 = var0.substring(0, 1).toUpperCase() + var0.substring(1);
        return var0;
    }

}
